package com.iqiyi.pps.epg.core.service.channel;

import com.iqiyi.pps.epg.core.async.AsyncService;
import com.iqiyi.pps.epg.core.async.IAsyncTask;
import com.iqiyi.pps.epg.core.model.channel.ChannelBase;
import com.iqiyi.pps.epg.core.model.channel.ChannelLog;
import com.iqiyi.pps.epg.core.model.channel.ChannelTag;
import com.iqiyi.pps.epg.core.model.program.ProgramData;
import com.iqiyi.pps.epg.core.service.notifymail.NotifyMailService;
import com.iqiyi.pps.epg.core.utils.Constants;
import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import com.iqiyi.pps.epg.core.utils.MailContentUtil;
import com.iqiyi.pps.epg.core.utils.mail.MailConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by karkarrotxu on 2015/12/15.
 */
@Service
public class ChannelAsyncSerivceImpl extends AsyncService implements IChannelAsyncService {
    @Autowired
    private NotifyMailService notifyMailService;
    @Autowired
    private ChannelService channelService;
    //200003919  正在直播
    //200978719  临时直播

    private static long DEFAULT_TAG1 = 200003919;
    private static long DEFAULT_TAG2 = 200978719;
    private static final Logger logger = LoggerFactory.getLogger(ChannelAsyncSerivceImpl.class);

    @Override
    public void autoChangeChannelTag(final long channelId){
        this.accept(new IAsyncTask() {
            @Override
            public void doTask() {
                setNewChannelTag(channelId);
            }
        });
    }
    protected boolean queryEnable() {
        return getEnable("channel");
    }

    private void setNewChannelTag(long channelId ) {
        logger.info("[setNewChannelTag][channelId={}] START", channelId);
        boolean flag = false;
        try {
            ChannelBase channelBase = channelService.getChannelBase(channelId);
            if (channelBase == null) {
                logger.info("channelBase is null .channelId ={}", channelId);
                return;
            }
            resetTag(channelId);
            saveChannelLog(channelId);
            sendNotifyTagChange(channelBase);
            channelBase.setIsSync(1);
            channelService.saveBase(channelBase);
            flag = true;
        } catch (Exception e) {
            logger.error("exception={}", e);
        }
        logger.info("[setNewChannelTag][channelId={}][flag={}]", channelId, flag);
    }



    private void resetTag(long channelId) {
        List<ChannelTag> channelTags = channelService.getChannelTag(channelId);
        ChannelTag channelTag = null;
        if (channelTags.size() == 0) {
            channelTag = new ChannelTag();
        } else {
            channelTag = channelTags.get(0);
        }
        int length = channelTags.size();
        for (int i = 1; i < length; i++) {
            channelService.deleteTag(channelTags.get(i));
        }
        channelTag.setTag1(DEFAULT_TAG1);
        channelTag.setTag2(DEFAULT_TAG2);
        channelService.saveTag(channelTag);
    }

    private void sendNotifyTagChange(ChannelBase channelBase) {
        Map<String, String> params = new HashMap<String, String>();
        params.put(MailConstant.C_QIPU_ID, channelBase.getQipuId() + "");
        params.put(MailConstant.C_NAME, channelBase.getName());
        params.put(MailConstant.C_ID, "" + channelBase.getId());
//        params.put(MailConstant.P_QIPU_ID, programData.getQipuId() + "");
//        params.put(MailConstant.P_NAME, programData.getPname());
//        params.put("startTime", DateTimeUtil.transLongFormat(programData.getStartPlayTime().getTime()));
//        params.put("endTime", DateTimeUtil.transLongFormat(programData.getStopPlayTime().getTime()));
        notifyMailService.addMailAsyncTask(MailConstant.TOS_TYPE_FROM_LOG, MailContentUtil.ContentType.CHANGE_TAG_REMIND.getTypeIndex(), params);
    }


    private void saveChannelLog(long channelId) {
        ChannelLog channelLog = new ChannelLog();
        channelLog.setFatherId(channelId);
        channelLog.setType(Constants.LogType.AUTO_SET_TAG.getIndex());
        channelLog.setDescr("tag:" + DEFAULT_TAG1 + "_" + DEFAULT_TAG2);
        channelLog.setUser("_system_");
        channelService.saveLog(channelLog);
    }
}
